package com.tdtxdcxm.ruralcommunity.slice;


import com.tdtxdcxm.ruralcommunity.ResourceTable;
import com.tdtxdcxm.ruralcommunity.appruntime.DataCenterControl;
import com.tdtxdcxm.ruralcommunity.httpmanagers.ThreadPoolUtil;
import com.tdtxdcxm.ruralcommunity.myutils.MyTools;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.TextField;
import ohos.net.NetHandle;
import ohos.net.NetManager;
import ohos.net.NetStatusCallback;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EditXmDetailSlice extends AbilitySlice{
    private Boolean toanotherslice = false;

    private Button writepage_xm_topddl_nobut,writepage_xm_topddl_gobut;
    private RadioContainer writepage_xm_radiocontainer;

    private TextField writepage_xm_rootdl_titletfd;

    private TextField writepage_xm_rootdl_nodeonetfd,writepage_xm_rootdl_nodetwotfd,writepage_xm_rootdl_nodethreetfd;

    private TextField writepage_xm_rootdl_tfd;

    /*<****************http构造表单上传动态发布指令数据区域*****************/
    public void UpDataXmInfo(String username){
        NetManager main_toplistnetmanger = NetManager.getInstance(this.getContext());
        if (!main_toplistnetmanger.hasDefaultNet()) {
            return;
        }
        ThreadPoolUtil.submit(() -> {
            NetHandle thenetHandle = main_toplistnetmanger.getDefaultNet();
            NetStatusCallback netStatusCallback = new NetStatusCallback() {};
            HttpURLConnection httpURLConnection = null;

            String urlString = DataCenterControl.serverIpWithPort + "pushcontent/?";
            try {
                URL url = new URL(urlString);

                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                URLConnection urlConnection = thenetHandle.openConnection(url, java.net.Proxy.NO_PROXY);
                if (urlConnection instanceof HttpURLConnection) {
                    httpURLConnection = (HttpURLConnection) urlConnection;

                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);

                    StringBuilder stringBuilder = new StringBuilder();

                    stringBuilder.append("username=");
                    stringBuilder.append(username);
                    stringBuilder.append("&type=");
                    stringBuilder.append(1); //0-dt,1-xm,2-wz
                    stringBuilder.append("&field=");
                    stringBuilder.append(writepage_xm_radiocontainer.getMarkedButtonId() + 1);

                    stringBuilder.append("&time=");
                    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = new Date(System.currentTimeMillis());
                    stringBuilder.append(formatter.format(date));

                    stringBuilder.append("&title=");
                    stringBuilder.append(writepage_xm_rootdl_titletfd.getText());
                    stringBuilder.append("&nodeone=");
                    stringBuilder.append(writepage_xm_rootdl_nodeonetfd.getText());
                    stringBuilder.append("&nodetwo=");
                    stringBuilder.append(writepage_xm_rootdl_nodetwotfd.getText());
                    stringBuilder.append("&nodethree=");
                    stringBuilder.append(writepage_xm_rootdl_nodethreetfd.getText());

                    stringBuilder.append("&detail=");
                    stringBuilder.append(writepage_xm_rootdl_tfd.getText());

                    byte[] b = stringBuilder.toString().getBytes();
                    httpURLConnection.getOutputStream().write(b);

                    try {
                        httpURLConnection.connect();
                    } catch (IOException e) {}
                    outputStream.flush();


                    InputStream inputStream = null;
                    try {
                        inputStream = urlConnection.getInputStream();
                        byte[] cache = new byte[2 * 1024];
                        int len = inputStream.read(cache);
                        while (len != -1) {
                            outputStream.write(cache, 0, len);
                            len = inputStream.read(cache);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String result = "";
                    result = outputStream.toString();

                    if(result.equals("OK")){
                        //发布项目成功
                        toanotherslice = true;
                        getUITaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                writepage_xm_rootdl_tfd.setText("");
                                writepage_xm_radiocontainer.cancelMarks();
                                writepage_xm_rootdl_titletfd.setText("");
                                writepage_xm_rootdl_nodeonetfd.setText("");
                                writepage_xm_rootdl_nodetwotfd.setText("");
                                writepage_xm_rootdl_nodethreetfd.setText("");
                                terminate();
                                MyTools.toastShow("项目发布成功！", EditXmDetailSlice.this.getContext());
                            }
                        });
                    }
                    else{
                        getUITaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                MyTools.toastShow("项目发布失败！", EditXmDetailSlice.this.getContext());
                            }
                        });

                    }
                }
            } catch (IOException e) {

            } finally {
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
            }
        });
    }
    /******************http构造表单上传动态发布指令数据区域***************>*/

    public void initDDSComponents(){
        final int[] nobutheight = {0};
        final boolean[] iskeyboarduse = {false};
        writepage_xm_topddl_nobut = (Button) findComponentById(ResourceTable.Id_writepage_xm_topddl_nobut);
        writepage_xm_topddl_gobut = (Button) findComponentById(ResourceTable.Id_writepage_xm_topddl_gobut);

        writepage_xm_radiocontainer = (RadioContainer) findComponentById(ResourceTable.Id_writepage_xm_radiocontainer);

        writepage_xm_rootdl_titletfd = (TextField) findComponentById(ResourceTable.Id_writepage_xm_rootdl_titletfd);

        writepage_xm_rootdl_nodeonetfd = (TextField) findComponentById(ResourceTable.Id_writepage_xm_rootdl_nodeonetfd);
        writepage_xm_rootdl_nodetwotfd = (TextField) findComponentById(ResourceTable.Id_writepage_xm_rootdl_nodetwotfd);
        writepage_xm_rootdl_nodethreetfd = (TextField) findComponentById(ResourceTable.Id_writepage_xm_rootdl_nodethreetfd);

        writepage_xm_rootdl_tfd = (TextField) findComponentById(ResourceTable.Id_writepage_xm_rootdl_tfd);

        writepage_xm_topddl_nobut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                writepage_xm_rootdl_tfd.setText("");
                writepage_xm_radiocontainer.cancelMarks();
                writepage_xm_rootdl_titletfd.setText("");
                writepage_xm_rootdl_nodeonetfd.setText("");
                writepage_xm_rootdl_nodetwotfd.setText("");
                writepage_xm_rootdl_nodethreetfd.setText("");
                MyTools.toastShow("取消发布项目！", EditXmDetailSlice.this.getContext());
                terminate();
            }
        });
        writepage_xm_topddl_gobut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                writepage_xm_rootdl_tfd.clearFocus();

                writepage_xm_rootdl_titletfd.clearFocus();
                writepage_xm_rootdl_nodeonetfd.clearFocus();
                writepage_xm_rootdl_nodetwotfd.clearFocus();
                writepage_xm_rootdl_nodethreetfd.clearFocus();

                if(writepage_xm_radiocontainer.getMarkedButtonId() == RadioContainer.INVALID_ID){
                    MyTools.toastShow("领域不能空！", EditXmDetailSlice.this.getContext());
                    return;
                }
                if(writepage_xm_rootdl_titletfd.getText().equals("")){
                    MyTools.toastShow("项目名称不能空！", EditXmDetailSlice.this.getContext());
                    return;
                }
                if(writepage_xm_rootdl_nodeonetfd.getText().equals("")){
                    MyTools.toastShow("项目节点1不能空！", EditXmDetailSlice.this.getContext());
                    return;
                }
                if(writepage_xm_rootdl_nodetwotfd.getText().equals("")){
                    MyTools.toastShow("项目节点2不能空！", EditXmDetailSlice.this.getContext());
                    return;
                }
                if(writepage_xm_rootdl_nodethreetfd.getText().equals("")){
                    MyTools.toastShow("项目节点3不能空！", EditXmDetailSlice.this.getContext());
                    return;
                }
                if(writepage_xm_rootdl_tfd.getText().equals("")){
                    MyTools.toastShow("内容不能空！", EditXmDetailSlice.this.getContext());
                    return;
                }
                UpDataXmInfo(DataCenterControl.userName);
            }
        });

        writepage_xm_topddl_nobut.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() {
            @Override
            public void onRefreshed(Component component) {
                if(nobutheight[0] == 0){
                    nobutheight[0] = writepage_xm_topddl_nobut.getHeight();
                    return;
                }
                if(writepage_xm_topddl_nobut.getHeight() < nobutheight[0]){
                    iskeyboarduse[0] = true;
                }
                if(writepage_xm_topddl_nobut.getHeight() == nobutheight[0] && iskeyboarduse[0] == true){
                    writepage_xm_rootdl_tfd.clearFocus();
                    iskeyboarduse[0] = false;
                }
            }
        });
    }


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main_xm);

        initDDSComponents();
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
